package pure.engine.model 
{
	import flash.geom.Rectangle;
	import pure.engine.model.RendererCarrier;
	import pure.engine.status.Status;
	import pure.engine.core.ns_despair;
	
	use namespace ns_despair;
	
	/**
	 * 实体
	 * [Author] - PURE
	 * 
	 * [Hint] - ① 当一个世界只有一个监视器时，有实体进入监视器范围，visible变为true.
	 *          ② 当一个世界存在多监视器时，无视监视器范围.
	 */
public class Entity extends Model2D
{        

	
	

	//======================
	// Property
	//====================== 
		
	
	// 名字
	public function get name():String { return m_name; }

	public function get id():int { return m_id; }

	
	
	// 真实区域
	public function get realRect():Rectangle
	{
		m_realRect.x = m_realX + m_x;
		m_realRect.y = m_realY + m_y;
		return m_realRect;
	}


	
	
	
	
	//======================
	// Base
	//======================

	// 真实体积
	public function setRealRect(rect:Rectangle):void
	{
		m_realRect = rect;
		m_realX = rect.x;
		m_realY = rect.y;
	}

	
	
	/**
	 * 安装组件
	 * 
	 * [Param] - C  实体组件
	 */
	public function setup(C:IEntityComponent):void
	{
		C.install(this);
	}
	

	
	

	
	
	//======================
	// Status !!
	//======================
	
	
	public function addStatus(status:Status):void
	{	
		status.setEntity(this);
		m_statusList.push(status);
	}
	
	
	public function removeStatus(index:int):void 
	{
		var S:Status;
		
		S = m_statusList.splice(index, index);
		S.dispose();
	}

	
	public function getStatus(index:int = 0):Status
	{
		return m_statusList[index];
	}
	
	
	public function clearAllStatus():void
	{
		var l:int;
		
		l = m_statusList.length;
		while (--l > -1)
		{
			m_statusList[l].dispose();
		}
		m_statusList.length = 0;
	}


	
	
	/* ----------------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------------
	 */
	
	

	
	/**
	 * 毁灭
	 */
	override ns_despair function destroy():void
	{
		super.destroy();
		
		EntityManager.unregister(this);
		clearAllStatus();
		m_interactive = true;
		m_realRect    =  null;
		cachedEntityList[numEntity++] = this;
	}

	
	/**
	 * 生成实体
	 * 
	 * [Param] - name
	 * [Param] - layer
	 */
	ns_despair static function getEntity(name:String):Entity
	{
		var E:Entity;
		
		if (numEntity > 0) 
		{
			numEntity--;
			E = cachedEntityList.pop();
		}
		else 
		{
			E = new Entity();
		}
		
		E.m_name = name;
		
		E.initialize();
		EntityManager.register(E);
		
		return E;
	}
	
	
	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair static var cachedEntityList:Vector.<Entity> = new Vector.<Entity>();
	
	ns_despair static var numEntity:int;
	
	ns_despair var m_realRect:Rectangle;  // 真实矩形（碰撞检测，会产生形变或透明度变化的渲染器通常不能使用）
	
	ns_despair var m_viewLeft:Number, m_viewRight:Number, m_viewTop:Number, m_viewBottom:Number;

	ns_despair var m_statusList:Array = [];


	ns_despair var m_realX:Number, m_realY:Number;
	

	
	
	

}
}

